<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8" >

<title>My AppBox V2.0 - 拦截器 | 没想好的个人博客</title>

<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no">

<!-- <link rel="stylesheet" href="https://use.fontawesome.com/releases/v5.7.2/css/all.css" integrity="sha384-fnmOCqbTlWIlj8LyTjo7mOUStjsKC4pOpQbqyi7RrhN7udi9RwhKkMHpvLbHG9Sr" crossorigin="anonymous"> -->
   <!-- Font Awesome CSS-->
   <link href="https://cdn.bootcss.com/font-awesome/5.13.0/css/all.css" rel="stylesheet">

   <link rel="stylesheet" href="https://cdn.bootcss.com/font-awesome/5.7.2/css/all.min.css" integrity="sha384-fnmOCqbTlWIlj8LyTjo7mOUStjsKC4pOpQbqyi7RrhN7udi9RwhKkMHpvLbHG9Sr" crossorigin="anonymous">

<link rel="shortcut icon" href="https://shixixiyue.gitee.io//favicon.ico?v=1709791195950">
<link rel="stylesheet" href="https://shixixiyue.gitee.io//styles/main.css">



<link rel="stylesheet" href="https://unpkg.com/aos@next/dist/aos.css" />
<script src="https://jsd.onmicrosoft.cn/npm/vue/dist/vue.js"></script>


<style>
  .hljs-ln{
    padding-bottom: 1.2rem;
    margin: 0rem !important;
  }
  .hljs-ln td {
    border: none !important;
    padding: 2px 6px !important;
    line-height: 1.2rem;
    font-size: 1rem;
  }
  .hljs-ln tr td:first-child  {
    color: cadetblue;
    border-right: 1px solid !important;
  }
  .hljs-ln tr {
    border: none !important;
  }
</style>

    <meta name="description" content="
之前介绍了My AppBox V2.0，今天把拦截器补上，适用FineUICore，实现登录超时跳转，url参数获取，以及操作日志的保存；









1. 继承扩展和引用
2. 超时登录的JS
3. 是否第一次回发
4. 上代码，..." />
    <meta name="keywords" content="FineUICore" />
  </head>
  <body>
    <div id="app" class="main">

      <div class="sidebar" :class="{ 'full-height': menuVisible }">
  <div class="top-container" data-aos="fade-right">
    <div class="top-header-container">
      <a class="site-title-container" href="https://shixixiyue.gitee.io/">
        <img src="https://shixixiyue.gitee.io//images/avatar.png?v=1709791195950" class="site-logo">
        <h1 class="site-title">没想好的个人博客</h1>
      </a>
      <div class="menu-btn" @click="menuVisible = !menuVisible">
        <div class="line"></div>
      </div>
    </div>
    <div>
      
        
          <a href="https://shixixiyue.gitee.io/" class="site-nav">
            首页
          </a>
        
      
        
          <a href="https://shixixiyue.gitee.io//tag/4gOKry8Qv/" class="site-nav">
            .Net Core
          </a>
        
      
        
          <a href="https://shixixiyue.gitee.io//tag/njETBpqXu/" class="site-nav">
            FineUICore
          </a>
        
      
        
          <a href="https://shixixiyue.gitee.io//tag/XyXyeLOBB" class="site-nav">
            读书笔记
          </a>
        
      
        
          <a href="https://shixixiyue.gitee.io//tag/TyVX6hfCJ" class="site-nav">
            Vue笔记
          </a>
        
      
        
          <a href="https://shixixiyue.gitee.io//tag/3Hz2sPtzO/" class="site-nav">
            产品
          </a>
        
      
        
          <a href="https://shixixiyue.gitee.io//tag/Db1wShB_E" class="site-nav">
            其他
          </a>
        
      
        
          <a href="https://shixixiyue.gitee.io/" class="site-nav">
            一一一一一一一一一
          </a>
        
      
        
          <a href="https://shixixiyue.gitee.io//archives" class="site-nav">
            文章归档
          </a>
        
      
        
          <a href="https://shixixiyue.gitee.io//tags" class="site-nav">
            全部标签
          </a>
        
      
    </div>
  </div>
  <div class="bottom-container" data-aos="flip-up" data-aos-offset="0">
    <div class="social-container">
      
        
      
        
      
        
      
        
      
        
      
    </div>
    <div class="site-description">
      没事啥都说
    </div>
    <div class="site-footer">
      QQ：<a href="tencent://message/?uin=935732994&Site=''&Menu=yes">935732994<a/> <a href="http://beian.miit.gov.cn">京ICP备2024045283号<a/> | <a class="rss" href="https://shixixiyue.gitee.io//atom.xml" target="_blank">RSS</a>
    </div>
  </div>
</div>


      <div class="main-container">
        <div class="content-container" data-aos="fade-up">
          <div class="post-detail">
            <h2 class="post-title">My AppBox V2.0 - 拦截器</h2>
            <div class="post-date">2019-06-28
            
                
                  <a href="https://shixixiyue.gitee.io/tag/njETBpqXu/" class="tag">
                    FineUICore
                  </a>
                
            </div>
            
            <div class="post-content" v-pre>
              <blockquote>
<p>之前介绍了<a href="index.html#2019060301.md">My AppBox V2.0</a>，今天把拦截器补上，适用<a href="https://core.fineui.com">FineUICore</a>，实现登录超时跳转，url参数获取，以及操作日志的保存；</p>
</blockquote>
<!-- more -->
<p><ul class="markdownIt-TOC">
<li>
<ul>
<li>
<ul>
<li>
<ul>
<li><a href="#1-%E7%BB%A7%E6%89%BF%E6%89%A9%E5%B1%95%E5%92%8C%E5%BC%95%E7%94%A8">1. 继承扩展和引用</a></li>
<li><a href="#2-%E8%B6%85%E6%97%B6%E7%99%BB%E5%BD%95%E7%9A%84js">2. 超时登录的JS</a></li>
<li><a href="#3-%E6%98%AF%E5%90%A6%E7%AC%AC%E4%B8%80%E6%AC%A1%E5%9B%9E%E5%8F%91">3. 是否第一次回发</a></li>
<li><a href="#4-%E4%B8%8A%E4%BB%A3%E7%A0%81%E6%B3%A8%E6%84%8F%E7%9C%8B%E6%B3%A8%E9%87%8A">4. 上代码，注意看注释</a></li>
</ul>
</li>
</ul>
</li>
</ul>
</li>
</ul>
</p>
<h4 id="1-继承扩展和引用">1. 继承扩展和引用</h4>
<p>在Controller层，所有页面都引用了 SysPageController ，所以拦截器的扩展只针对 SysPageController 类就可以了，在 System.Web.Mvc 中扩展拦截器直接继承 Controller 类，扩展 OnResultExecuted 方法，在 Microsoft.AspNetCore.Mvc 中挪了位置，要继承 ActionFilterAttribute 类中的 OnResultExecuted 方法：</p>
<pre><code>/// &lt;summary&gt;
/// 回发权限控制
/// &lt;/summary&gt;
public class SysPageFilter : ActionFilterAttribute
{
    #region 回发权限控制 OnResultExecuted
    /// &lt;summary&gt;
    /// 覆盖
    /// &lt;/summary&gt;
    /// &lt;param name=&quot;filterContext&quot;&gt;&lt;/param&gt;
    public override void OnResultExecuted(ResultExecutedContext filterContext)
    {
        //
        base.OnResultExecuted(filterContext);
    }
}
</code></pre>
<p>需要在 SysPageController 头上添加引用：</p>
<pre><code>/// &lt;summary&gt;
/// 拦截器
/// &lt;/summary&gt; 
[SysPageFilter] //注解
public class SysPageController : BaseController
{
    //
}
</code></pre>
<h4 id="2-超时登录的js">2. 超时登录的JS</h4>
<p>当用户登录超时（直接判断Session在不在），需要弹出是否重新登录的提示，只需要抛出一段js就行了，执行的并不是跳转页面，而是触发顶层框架上的退出按钮方法：</p>
<pre><code>//得到重新登录的js
private string GetSysLoginJs()
{
    //确定后执行的js
    string js = &quot;top.window.F.doPostBack({url: 'btnHomePage_Click',disableControl: 'btnHomePage'}); &quot;;
    string conjs = &quot;&quot;;
    //如果顶层已经弹出了就不在此弹出了
    conjs += &quot;if(top.$('div.relodeMsg.f-messagebox-alert').length==0){&quot;;
    Alert a = new Alert();
    a.CssClass = &quot;relodeMsg&quot;;
    a.Message = &quot;登录已过期&lt;br/&gt;点击【确定】跳转到登录页面&quot;;
    a.Title = &quot;提示&quot;;
    a.OkScript = js;
    a.IconFont = IconFont.Warning;
    a.Target = Target.Top;
    conjs += a.GetShowReference();
    conjs += &quot;}&quot;;
    return conjs;
}
</code></pre>
<h4 id="3-是否第一次回发">3. 是否第一次回发</h4>
<p>我还为 HttpRequest 扩展了一个方法，判断是否是 Ajax 回发，如果为真则认为是点击按钮触发的拦截，与打开菜单区分</p>
<pre><code>/// &lt;summary&gt;
/// HttpRequest 扩展
/// &lt;/summary&gt;
public static class HttpRequestEx
{
    /// &lt;summary&gt;
    /// 是否是ajax回发
    /// &lt;/summary&gt;
    /// &lt;param name=&quot;req&quot;&gt;&lt;/param&gt;
    /// &lt;returns&gt;&lt;/returns&gt;
    public static bool IsAjax(this HttpRequest req)
    {
        bool result = false;
        //请求头标识
        var xreq = req.Headers.ContainsKey(&quot;x-requested-with&quot;);
        if (xreq)
        {
            result = req.Headers[&quot;x-requested-with&quot;] == &quot;XMLHttpRequest&quot;;
        }

        return result;
    }
}
</code></pre>
<h4 id="4-上代码注意看注释">4. 上代码，注意看注释</h4>
<p>没有难点，注意看注释</p>
<pre><code>internal class SysPageFilter : ActionFilterAttribute
{
/// &lt;summary&gt;
/// 覆盖
/// &lt;/summary&gt;
/// &lt;param name=&quot;filterContext&quot;&gt;&lt;/param&gt;
public override void OnResultExecuted(ResultExecutedContext filterContext)
{
    //得到用户
    var value = filterContext.HttpContext.Session.GetString(&quot;LoginUser&quot;);
    if (string.IsNullOrEmpty(value))
    {
        string conjs = GetSysLoginJs();
        PageContext.RegisterStartupScript(conjs);
        return;
    }
    JObject _value = JObject.Parse(value);
    Mssystem_1 lodeuser = (Mssystem_1)_value.ToObject(typeof(Mssystem_1));

    //为空时跳转 注意当前页面不是登录页面
    if (filterContext.RouteData.Values.Values.ToList()[1].ToString() != &quot;SysLogin&quot; &amp;&amp; lodeuser == null)
    {
        string conjs = GetSysLoginJs();
        PageContext.RegisterStartupScript(conjs);
        return;
    }
    //是否第一次回发， 即加载页面
    if (!filterContext.HttpContext.Request.IsAjax())
    {
        string curMenuId = &quot;&quot;;
        //得到当前菜单ID
        curMenuId = filterContext.HttpContext.Request.Query[&quot;MenuId&quot;];
        if (!string.IsNullOrEmpty(curMenuId))
        {
            bool b = true;//按钮权限开关，默认开
            //如果是超级管理员，直接跳过权限
            if (lodeuser.isadmin)
            {
                b = false;
            }
            if (b)
            {
                //判断该用户是否有该菜单权限
                if (!lodeuser.menuids.Contains(Convert.ToInt32(curMenuId)))
                {
                    string conjs = GetSysLoginJs();
                    PageContext.RegisterStartupScript(conjs);
                    return;
                }
                //得到数据库中 用户 菜单权限 显示的按钮ID 
                List&lt;string&gt; btnids = BLLsystem_1.GetUserBtnByMenu(lodeuser.sys10.System_10_10, curMenuId, false);
                if (btnids != null)
                {
                    StringBuilder sb = new StringBuilder();
                    foreach (string item in btnids)
                    {
                        //隐藏 也可以执行除隐藏之外的代码
                        sb.Append(&quot;if(F.ui.&quot; + item + &quot;){F.ui.&quot; + item + &quot;.hide();};&quot;);
                    }
                    //抛出 
                    PageContext.RegisterStartupScript(sb.ToString());
                }
            }
            //记录日志 用户点击菜单
            BLLsystem_16.ClickMenuLog(lodeuser.System_1_10.ToString(), curMenuId);
        }
    }
    //如果不用是第一次回发 记录点击的按钮
    else
    {
        //加入算法获取MenuId
        string MenuId = &quot;&quot;;
        //参数很难获取，谁会赶紧告诉我
        string url = ((HttpRequestHeaders)((DefaultHttpRequest)filterContext.HttpContext.Request).Headers).HeaderReferer;
        string query = url.Split(&quot;?&quot;)[1];
        foreach (var item in query.Split('&amp;'))
        {
            foreach (var _item in item.Split('='))
            {
                if (_item == &quot;MenuId&quot;)
                {
                    MenuId = item.Split('=')[1];
                }
            }
        }
        if (!string.IsNullOrEmpty(MenuId))
        {
            //触发事件名称，由后台判断是否是一个按钮方法
            string streve = filterContext.RouteData.Values.Values.ToList()[2].ToString();
            //记录日志 用户点击按钮
            BLLsystem_16.ButtonLog(lodeuser.System_1_10.ToString(), MenuId, streve);
        }
    }
    base.OnResultExecuted(filterContext);
}
}
</code></pre>
<figure data-type="image" tabindex="1"><img src="https://shixixiyue.gitee.io//post-images/1684482136255.png" alt="日志" loading="lazy"></figure>

            </div>
            
              <div class="tag-container">
                
                  <a href="https://shixixiyue.gitee.io/tag/njETBpqXu/" class="tag">
                    FineUICore
                  </a>
                
              </div>
            
            
              <div class="next-post">
                <div class="next">下一篇</div>
                <a href="https://shixixiyue.gitee.io/c-winform-shou-ji/">
                  <h3 class="post-title">
                    C# Winform手记
                  </h3>
                </a>
              </div>
            

            

          </div>

        </div>
      </div>
    </div>

    <script src="https://unpkg.com/aos@next/dist/aos.js"></script>
<script type="application/javascript">

AOS.init();

var app = new Vue({
  el: '#app',
  data: {
    menuVisible: false,
  },
})

</script>


  <script src="//jsd.onmicrosoft.cn/gh/highlightjs/cdn-release@11.5.1/build/highlight.min.js"></script>
  <script>
    hljs.initHighlightingOnLoad();
    //hljs.registerLanguage('mermaid', window.hljsDefineMermaid);
  </script>
  <script src="//jsd.onmicrosoft.cn/npm/highlightjs-line-numbers.js@2.8.0/dist/highlightjs-line-numbers.min.js"></script>
  <script>
    hljs.initLineNumbersOnLoad({
      singleLine: true
    });
  </script>
 <script src="https://jsd.onmicrosoft.cn/npm/mermaid/dist/mermaid.min.js"></script>
   <script>
    mermaid.initialize({
      startOnLoad: true,
      securityLevel: 'loose',
    });
  </script>





  </body>
</html>
